
$('.del-user').click(function () {
    var that=this;
    swal({
            title: "您确定要删除此用户吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的，我要删除！",
            cancelButtonText: "让我再考虑一下…",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                var userId=$(that).parent().next(".userId").val()
                $.ajax({
                    async:true,
                    url:"/admin/users",
                    type:'DELETE',//请求的方式
                    contentType: "application/json",
                    data:JSON.stringify({ "userId": userId }),//请求的数据
                    success:function (res) {//请求成功后返回的数据会封装在回调函数的第一个参数中
                        //通过backdata来获取所需要的数据
                        if(res.success){
                            swal({title:"删除成功！", text:"您已经永久删除了这条信息。", type:"success"},function () {
                                location.reload();
                            });
                        }
                    },
                    error:function (xhr) {//响应不成功时返回的函数
                        var errMsg=(xhr.responseJSON.message||xhr.responseJSON.msg)
                        if(xhr.status==403){
                            errMsg='你没有权限执行当前操作！！'
                            toastr["warning"](errMsg);
                        }else if(xhr.status==500){
                            toastr["error"](errMsg);
                        }

                    },
                    dataType:'json'//设置返回的数据类型
                })

            } else {
                swal("已取消", "您取消了删除操作！", "error");
            }
        });
});

var config = {
    '.chosen-select': {},
    '.chosen-select-deselect': {
        allow_single_deselect: true
    },
    '.chosen-select-no-single': {
        disable_search_threshold: 10
    },
    '.chosen-select-no-results': {
        no_results_text: 'Oops, nothing found!'
    },
    '.chosen-select-width': {
        width: "95%"
    }
}
for (var selector in config) {
    $(selector).chosen(config[selector]);
}


// $('#addAdmin').on('hidden.bs.modal', function (e) {
// })


$.validator.setDefaults({
    submitHandler: function(form) {
        $(form).ajaxSubmit({
            success:  successCallback,  // 提交后
            error : function(xhr, status, err) {
                var errMsg=(xhr.responseJSON.message||xhr.responseJSON.msg)
                if(xhr.status==403){
                    errMsg='你没有权限执行当前操作！！'
                }
                $("#errTip").parent().css({"display":"block"});
                $("#errTip").html("<i class=\"fa fa-info-circle\"></i>"+errMsg);
            },
            resetForm: true        // 成功提交后，重置所有的表单元素的值.
        });
    }
});

$.validator.addMethod("chosen-limit",function(value,element,param){
    if($(".search-choice").children('span').length>0){
        return true
    }else{
        return false
    }
},"<i class='fa fa-times-circle'></i>至少选择一个角色");



function successCallback(data, status, $form)  {
    $("#addAdmin").modal("hide");
    location.reload();

}
//以下为官方示例
$().ready(function () {
    //新增管理员表单
    $("#addUserForm").on("submit",function () { //触发form表单提交，url写在form标签
        $("#addUserForm").validate({
            //通过验证后运行的函数，里面要加上表单提交的函数，否则表单不会提交。
        })
        return false; // 必须返回false，否则表单会自己再做一次提交操作，并且页面跳转
    });

    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#addUserForm").validate({
        ignore: ":hidden:not(select)",
        rules: {
            username: "required",
            phone: {
                required: true
            },
            password: {
                required: true
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            roles:"chosen-limit",
            email: "required",
            address: "required"
        },
        messages: {
            username: {
                required: icon + "请输入用户名"
            },
            phone:{
                required: icon+"请输入手机号"
            },
            password: {
                required: icon+"请输入密码",
                minlength: "密码长度不能小于 5 个字母"
            },
            confirm_password: {
                required: icon+"请再次输入密码",
                minlength: icon+"密码长度不能小于 5 个字母",
                equalTo: icon+"两次密码输入不一致"
            },
            email:{
                required: icon+"请输入邮箱"
            },
            address:{
                required: icon+"请输入联系地址"
            }
        },
        errorPlacement:function (error, element) {
            if(error[0].id.indexOf("roles")!=-1){
                $("#roles").after(error);
                $(error).css({"position":"relative","right":"40px"})
            }else{
                error.appendTo(element.parent());
            }

        }
    });

});